package com.xiaoshuidi.cloud.module.finance.service.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.account.FinanceAccountBalanceWarningReqVO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceAccountBalanceWarningDO;
import com.xiaoshuidi.cloud.module.finance.dal.mysql.finance.FinanceAccountBalanceWarningMapper;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceAccountBalanceWarningService;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FinanceAccountBalanceWarningServiceImpl implements FinanceAccountBalanceWarningService {
    @Resource
    private FinanceAccountBalanceWarningMapper financeAccountBalanceWarningMapper;
    @Resource
    private RedissonClient redissonClient;

    @Override
    public FinanceAccountBalanceWarningDO getByCustType(String custType) {
        List<FinanceAccountBalanceWarningDO> l = financeAccountBalanceWarningMapper.selectList(new QueryWrapper<FinanceAccountBalanceWarningDO>().eq("cust_type", custType).orderByDesc("id"));
        return CollectionUtils.isEmpty(l) ? null : l.get(0);
    }

    @Override
    public void update(Integer id, FinanceAccountBalanceWarningReqVO reqVO, Long tenantId) {
        FinanceAccountBalanceWarningDO update = FinanceAccountBalanceWarningDO.builder()
                .id(id)
                .warningValue(reqVO.getWarningValue())
                .custType(reqVO.getCustType())
                .remark(reqVO.getRemark())
                .notificationInterval(reqVO.getNotificationInterval())
                .build();
        financeAccountBalanceWarningMapper.updateById(update);
        cache(tenantId, update);
    }

    @Override
    public void save(FinanceAccountBalanceWarningReqVO reqVO, Long tenantId) {
        FinanceAccountBalanceWarningDO e = FinanceAccountBalanceWarningDO.builder()
                .warningValue(reqVO.getWarningValue())
                .custType(reqVO.getCustType())
                .remark(reqVO.getRemark())
                .notificationInterval(reqVO.getNotificationInterval())
                .build();
        financeAccountBalanceWarningMapper.insert(e);
        cache(tenantId, e);
    }

    @Override
    public FinanceAccountBalanceWarningDO getWarningValue(Long tenantId, String custType) {
        RMap<String, FinanceAccountBalanceWarningDO> m = redissonClient.getMap("lease:account:warning:" + tenantId);
        FinanceAccountBalanceWarningDO e = m.get(custType);
        if (e != null) {
            return e;
        }
        e = getByCustType(custType);
        if (e != null) {
            cache(tenantId, e);
        }
        return e;
    }

    private void cache(Long tenantId, FinanceAccountBalanceWarningDO e) {
        redissonClient.getMap("lease:account:warning:" + tenantId)
                .put(e.getCustType(), e);
    }

}
